package eleme.openapi.sdk.oauth;

import eleme.openapi.sdk.config.Config;
import eleme.openapi.sdk.config.Constants;
import eleme.openapi.sdk.oauth.impl.DefaultOAuthClient;
import eleme.openapi.sdk.oauth.impl.ServerOAuthCodeImpl;
import eleme.openapi.sdk.oauth.request.ClientTokenRequest;
import eleme.openapi.sdk.oauth.request.ServerOpenIdRequest;
import eleme.openapi.sdk.oauth.request.ServerRefreshTokenRequest;
import eleme.openapi.sdk.oauth.request.ServerTokenRequest;
import eleme.openapi.sdk.oauth.response.OpenId;
import eleme.openapi.sdk.oauth.response.Token;

/**
 * @author me
 */
public class OAuthClient {

    private Config config;

    public OAuthClient(Config config) {
        this.config = config;
    }

    private IOAuthClient oauthClient = null;

    /**
     * 客户端授权模式获取Token
     *
     * @return Token信息
     */
    public Token getTokenInClientCredentials() {
        oauthClient = new DefaultOAuthClient(config, Constants.TOKEN_URL);
        ClientTokenRequest oAuthRequest = new ClientTokenRequest(config);
        Token token = oauthClient.execute(oAuthRequest);
        return token;
    }

    /**
     * 构造授权URL
     *
     * @param redirectUri 重定向地址
     * @param scope 申请的权限范围
     * @param state 客户端当前状态
     * @return 授权URL
     */
    public String getAuthUrl(String redirectUri, String scope, String state) {
        ServerOAuthCodeImpl serverOAuthCode = new ServerOAuthCodeImpl(
                config.getOauthCodeUrl(),
                config.getAppKey());
        return serverOAuthCode.getAuthUrl(redirectUri, scope, state);
    }

    /**
     * 授权码模式获取Token
     *
     * @param authCode 授权码
     * @param redirectUri 重定向地址
     * @return Token信息
     */
    public Token getTokenByCode(String authCode, String redirectUri) {
        oauthClient = new DefaultOAuthClient(config, Constants.TOKEN_URL);
        ServerTokenRequest serverTokenRequest = new ServerTokenRequest(config);
        serverTokenRequest.setCode(authCode);
        serverTokenRequest.setRedirectUri(redirectUri);
        Token token = oauthClient.execute(serverTokenRequest);
        return token;
    }

    /**
     * 刷新Token
     *
     * @param refreshToken refreshToken
     * @return Token信息
     */
    public Token getTokenByRefreshToken(String refreshToken) {
        oauthClient = new DefaultOAuthClient(config, Constants.TOKEN_URL);
        ServerRefreshTokenRequest refreshTokenRequest = new ServerRefreshTokenRequest(config);
        refreshTokenRequest.setRefreshToken(refreshToken);
        Token token = oauthClient.execute(refreshTokenRequest);
        return token;
    }

    /**
     * 授权码换OpenId
     *
     * @param code 授权码
     * @param redirectUri 重定向地址
     * @return OpenId信息
     */
    public OpenId getOpenIdByCode(String code, String redirectUri) {
        oauthClient = new DefaultOAuthClient(config, Constants.OPENID_URL);
        ServerOpenIdRequest serverOpenIdRequest = new ServerOpenIdRequest(config);
        serverOpenIdRequest.setCode(code);
        serverOpenIdRequest.setRedirectUri(redirectUri);
        OpenId openId = oauthClient.execute(serverOpenIdRequest);
        return openId;
    }

}
